package com.wf.smartcomm.server.daoImpl;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import com.wf.smartcomm.server.bean.Merchant;
import com.wf.smartcomm.server.dao.MerchantDao;
import com.wf.smartcomm.server.model.Page;
import com.wf.smartcomm.server.util.PageUtils;

@Repository
public class MerchantDaoImpl implements MerchantDao {

	@Resource
	private SessionFactory sessionFactory;

	@Override
	public Page list(Page page, int type) throws Exception {
		Session session = sessionFactory.getCurrentSession();
		if (type == 0) {
			Query query = session.createQuery("select count(*) from Merchant");
			Long counts = (Long) query.uniqueResult();
			page = PageUtils.createPage(page, counts.intValue());
			query = session.createQuery("from Merchant order by createdAt desc");
			query.setFirstResult(page.getBeginIndex());
			query.setMaxResults(page.getPageSize());
			page.setDataList(query.list());
		}else {
			Query query = session.createQuery("select count(*) from Merchant where type =:type");
			query.setParameter("type",type);
			Long counts = (Long) query.uniqueResult();
			page = PageUtils.createPage(page, counts.intValue());
			query = session.createQuery("from Merchant merchant where merchant.type=:type order by createdAt desc");
			query.setParameter("type",type);
			query.setFirstResult(page.getBeginIndex());
			query.setMaxResults(page.getPageSize());
			page.setDataList(query.list());
		}
		
		session.flush();
		return page;
	}
	
	

	

}
